package com.hue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hue.entity.Orders;
import com.hue.entity.SeckillGoods;
import com.hue.entity.SeckillOrder;
import com.hue.mapper.SeckillOrderMapper;
import com.hue.service.SeckillGoodsService;
import com.hue.service.SeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-07-16
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements SeckillOrderService {
    @Autowired
    private SeckillGoodsService seckillGoodsService ;

    @Override
    public void createOrder(Orders orders) {
        //生成秒杀订单表
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(orders.getUserId());
        seckillOrder.setGoodsId(orders.getGoodsId());
        seckillOrder.setOrderId(orders.getId());
        save(seckillOrder);
        //秒杀库存-1
        QueryWrapper<SeckillGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsId", orders.getGoodsId());
        SeckillGoods one = seckillGoodsService.getOne(wrapper);
        one.setStockCount(one.getStockCount()-1);
        seckillGoodsService.updateById(one);
    }
}
